package cn.iocoder.yudao.module.system.controller.admin.workrack;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.workrack.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workrack.WorkRackDO;
import cn.iocoder.yudao.module.system.service.workrack.WorkRackService;

@Tag(name = "管理后台 - 上挂记录")
@RestController
@RequestMapping("/system/work-rack")
@Validated
public class WorkRackController {

    @Resource
    private WorkRackService workRackService;

    @PostMapping("/create")
    @Operation(summary = "创建上挂记录")
    @PreAuthorize("@ss.hasPermission('system:work-rack:create')")
    public CommonResult<Integer> createWorkRack(@Valid @RequestBody WorkRackSaveReqVO createReqVO) {
        return success(workRackService.createWorkRack(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新上挂记录")
    @PreAuthorize("@ss.hasPermission('system:work-rack:update')")
    public CommonResult<Boolean> updateWorkRack(@Valid @RequestBody WorkRackSaveReqVO updateReqVO) {
        workRackService.updateWorkRack(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除上挂记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-rack:delete')")
    public CommonResult<Boolean> deleteWorkRack(@RequestParam("id") Integer id) {
        workRackService.deleteWorkRack(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除上挂记录")
                @PreAuthorize("@ss.hasPermission('system:work-rack:delete')")
    public CommonResult<Boolean> deleteWorkRackList(@RequestParam("ids") List<Integer> ids) {
        workRackService.deleteWorkRackListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得上挂记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-rack:query')")
    public CommonResult<WorkRackRespVO> getWorkRack(@RequestParam("id") Integer id) {
        WorkRackDO workRack = workRackService.getWorkRack(id);
        return success(BeanUtils.toBean(workRack, WorkRackRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得上挂记录分页")
    @PreAuthorize("@ss.hasPermission('system:work-rack:query')")
    public CommonResult<PageResult<WorkRackRespVO>> getWorkRackPage(@Valid WorkRackPageReqVO pageReqVO) {
        PageResult<WorkRackDO> pageResult = workRackService.getWorkRackPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkRackRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出上挂记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:work-rack:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkRackExcel(@Valid WorkRackPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkRackDO> list = workRackService.getWorkRackPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "上挂记录.xls", "数据", WorkRackRespVO.class,
                        BeanUtils.toBean(list, WorkRackRespVO.class));
    }

}